﻿package cn.antscript.FaceDetector.view.component 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author AntScript
     * @blog  http://www.antscript.cn
	 */
	public class PhotoPanel extends Sprite
	{
		private var _photos:Vector.<Sprite>;
		private var _photosContainer:Sprite;
		
		private var _bitmapdata:BitmapData;
		
		public function PhotoPanel() 
		{
			init();
		}
		
		private function init():void
		{
			initPanel();
		}
		
		private function initPanel():void
		{
			_photosContainer = new Sprite();
			addChild(_photosContainer);
			
			_photos = Vector.<Sprite>([]);
			
		}
		
		public function addPhoto(photo:Bitmap):void {
			var container:Sprite = new Sprite();
			photo.scaleX = photo.scaleY = 0.25;
			container.addChild(photo);
			_photos.push(container);
			container.mouseChildren = false;
			container.buttonMode = true;
			draw();
			container.addEventListener(MouseEvent.CLICK, onClick);
			container.x = (_photos.length-1) * 90+10;
			_photosContainer.addChild(container);
			if (_photosContainer.width > 640) {
				TweenLite.to(_photosContainer, 1, { x:640 - _photosContainer.width } );
			}
		}		
		
		private function onClick(e:MouseEvent):void 
		{
			_bitmapdata = (e.target.getChildAt(0) as Bitmap).bitmapData;
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		private function draw():void {
			_photosContainer.graphics.clear();
			_photosContainer.graphics.beginFill(0x999999);
			_photosContainer.graphics.drawRect( -10, -10, _photos.length * 90 + 20, 80);
			_photosContainer.graphics.endFill();
		}
		
		
		public function get bitmapdata():BitmapData {
			return _bitmapdata;
		}
		
	}
	
}